package com.qau.zek.controller;

import com.qau.zek.pojo.User;
import com.qau.zek.utils.JDBCUtil;
import com.qau.zek.utils.JSONUtil;
import com.qau.zek.view.DeptCountView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/user")
public class User_main_controller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.service()");
        //解决post请求中文乱码问题
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        System.out.println(method);
        switch (method) {
            case "selectDeptCount":
                selectDeptCount(req, resp);
                break;
            case "login":
                login(req, resp);
                break;
            case "logout":
                logout(req, resp);
                break;
            default:
                break;
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.logout()");
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    }


    private void login(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.login()");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select * from user where username=? and password=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            System.out.println(statement);
            resultSet = statement.executeQuery();

            while (resultSet.next()) {
//                int id = resultSet.getInt("id");
                String name = resultSet.getString("username");
                String pwd = resultSet.getString("password");
                System.out.println(name);
                System.out.println(pwd);
                System.out.println("1111111111111111");
                user = new User(name, pwd);
                System.out.println(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (user == null) {
            //这个用户不存在
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("isOk", false);
            JSONUtil.obj2Json(map, resp);

        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("isOk", true);
            JSONUtil.obj2Json(map, resp);
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
        }


    }

    private void selectDeptCount(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.selectDeptCount()");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<DeptCountView> list = new ArrayList<DeptCountView>();
        try {
            connection = JDBCUtil.getConnection();
            String sql =
                    "SELECT '商品数' as name,COUNT(good_id) as value FROM goods\n" +
                            "UNION\n" +
                            "SELECT '商店数' as name,COUNT(shop_id) as value FROM shops\n" +
                            "UNION\n" +
                            "SELECT '供应商数' as name ,COUNT(sup_id) as value FROM suppliers;\n";

            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                int value = resultSet.getInt("value");
                DeptCountView deptCountVO = new DeptCountView(name, value);
                list.add(deptCountVO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (DeptCountView deptCountView : list) {
            System.out.println(deptCountView);
        }

        JSONUtil.array2Json(list, resp);
    }


}
